//
//  IMJSONKitViewController.m
//  IosFrameWork
//
//  Created by imaginedays on 13-4-1.
//  Copyright (c) 2013年 imagindays. All rights reserved.
//

#import "IMJSONKitViewController.h"
#import "JSONKit.h"

@interface IMJSONKitViewController ()

@end

@implementation IMJSONKitViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)beginJsonParser:(id)sender {
    // simple dictionary
    JSONDecoder *decoder = [[JSONDecoder alloc]init];
    NSDictionary *simpleDictionary = [decoder objectWithData:[self getSimpleJSON]];
    _mSimpleDictionay.text = [simpleDictionary objectForKey:@"person"];
    
    // nestedDictionary
    NSDictionary *nestedDictionary = [decoder objectWithData:[self getNestedJSON]];
    NSDictionary *personDictionary = [nestedDictionary objectForKey:@"person"];
    _mNetedDictionary.text = [NSString stringWithFormat:@"%@,%@",[personDictionary objectForKey:@"name"],[personDictionary objectForKey:@"job"]];
    
    //listDictionary
    NSDictionary *listDictionary = [decoder objectWithData:[self getJSONList]];
    NSArray *people = [listDictionary objectForKey:@"directory"];
    
    NSMutableString *mutableStr = [[NSMutableString alloc]init];
    
    for (NSDictionary *person in people) {
        NSDictionary *personDetails = [person objectForKey:@"person"];
        
        [mutableStr appendFormat:@"%@,%@",[personDetails objectForKey:@"name"],[personDetails objectForKey:@"job"]];
        
    }
    
    _mListDictionary.text = mutableStr;
    
}


- (NSData*) getSimpleJSON {
    NSString* jsonString = @"{ \
    \"person\": \"Usman\" \
    }";
    return  [jsonString dataUsingEncoding:NSUTF8StringEncoding];
}

- (NSData*) getNestedJSON {
    NSString* jsonString = @"{                      \
    \"person\": {                               \
    \"name\": \"Usman\",                    \
    \"job\": \"Software Engineer\"   \
    }                                           \
    }";
    return  [jsonString dataUsingEncoding:NSUTF8StringEncoding];
}

- (NSData*) getJSONList {
    NSString* jsonString = @"                                       \
    {                                                               \
    \"directory\": [                                            \
    {                                             \
    \"person\": {                            \
    \"name\": \"Usman\",                  \
    \"job\": \"Software Engineer\"        \
    }                                         \
    },                                            \
    {                                             \
    \"person\": {                            \
    \"name\": \"Someone else\",           \
    \"job\": \"Some other profession\"    \
    }                                         \
    }                                             \
    ]                                                   \
    }                                                               \
    ";
    return  [jsonString dataUsingEncoding:NSUTF8StringEncoding];
}

@end
